#include <func.h>


//子线程申请的空间可以在另外一个函数释放
void cleanup1(void *p)
{
    printf("I am child thread,start free\n");
    free(p);
    printf("free success\n");
}

void* thread_func(void *p)
{
    void *p1=malloc(20);
    pthread_cleanup_push(cleanup1,p1);
    sleep(1);
    pthread_cleanup_pop(1);
    printf("I am child,正常退出\n");
    return NULL;
}

int main ()
{
    pthread_t th_id;
    int ret=pthread_create(&th_id,NULL,thread_func,NULL);
    THREAD_ERR_CHECK(ret,"pthread_create");
    printf("I am main thread\n");

    ret=pthread_cancel(th_id);
    THREAD_ERR_CHECK(ret,"pthread_cancel");
    long thread_ret;
    ret=pthread_join(th_id,(void**)&thread_ret);
    THREAD_ERR_CHECK(ret,"pthread_join");
    printf("I am main thread %ld\n",thread_ret);
    return 0;
}